package tech.treec.smartlight.activity;

import android.Manifest;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


import com.young.mylib.utils.TimeUtils;
import com.young.mylib.utils.Utils;
import com.young.mylib.view.MyDatePickerDialog;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;

import tech.treec.smartlight.MyApplication;
import tech.treec.smartlight.R;
import tech.treec.smartlight.entity.EventMessage;
import tech.treec.smartlight.service.DeviceService;
import tech.treec.smartlight.service.MQTTService;

import static tech.treec.smartlight.Constants.ACTION_BASE_RECEIVE;
import static tech.treec.smartlight.Constants.ACTION_MQTT_RESTART_SERVICE;


/**
 * Created by Young on 2017/11/7.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private final static String TAG = "BaseActivity";
    MyApplication application;
    private Toast toast;
    private View toastView;
    private TextView tv_toast;

    private ProgressDialog mProgressDialog = null;
    protected boolean progressDialogShow = false;
    boolean isExit;
    private String timeoutMsg = "";

    private MsgReceiver msgReceiver;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int monthOfYear = calendar.get(Calendar.MONTH);
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    protected Context context;
    private static final int REQUEST_CODE = 101;

    int[][] states = new int[][]{

            new int[]{android.R.attr.state_activated}, // enabled
            new int[]{android.R.attr.state_active}, // disabled
            new int[]{android.R.attr.state_checked}, // unchecked
            new int[]{android.R.attr.state_focused}, // pressed
            new int[]{android.R.attr.state_enabled}, // enabled
            new int[]{-android.R.attr.state_enabled}, // disabled
            new int[]{-android.R.attr.state_checked}, // unchecked
            new int[]{android.R.attr.state_pressed}  // pressed
    };
    int[] colors = new int[]{

            Color.BLACK,
            Color.YELLOW,
            Color.YELLOW,
            Color.YELLOW,
            Color.BLACK,
            Color.BLACK,
            Color.YELLOW,
            Color.BLACK
//              ContextCompat.getColor(this,R.color.colorPrimary),

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(setLayoutID());
        ButterKnife.bind(this);
        Log.i(TAG, "base activity,start");
        application = MyApplication.getInstance();
        context = this;
        toast = new Toast(getApplicationContext());
        toastView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.toast_activity, null);
        tv_toast = (TextView) toastView.findViewById(R.id.tv_toast);

        init(savedInstanceState);
        initView();
        loadData();


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
        ) {
            //    ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST_MULTIPLE);
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.INTERNET,
            }, REQUEST_CODE);
        } else {
            // 权限已经授予，执行相应操作
            performActionWithPermission();
        }
        if (msgReceiver == null) {
            msgReceiver = new MsgReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ACTION_BASE_RECEIVE);
            registerReceiver(msgReceiver, intentFilter);
        }

        if (!Utils.isServiceWork(BaseActivity.this, "tech.treec.smartlight.service.DeviceService")) {
            Intent svc = new Intent(BaseActivity.this, DeviceService.class);
            startService(svc);
        }

        restartMqttService();

        //  DeviceDao.dropDeviceDataTable();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，执行操作
                performActionWithPermission();
            } else {
                // 权限被拒绝，提示用户手动开启权限或给出相应提示
                //  Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void performActionWithPermission() {
        // 这里放置需要权限的操作，例如读取存储空间等
        //  Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
    }

    protected abstract int setLayoutID();

    protected abstract void init(Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract void loadData();

    @Override
    protected void onResume() {
        super.onResume();
//        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
    }

    public void showProgressDialog(String message, boolean cancelable) {
        timeoutMsg = message;
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);

            // mProgressDialog.setProgressStyle(R.style.dialog);
        }

        if (TextUtils.isEmpty(message)) {
            message = "";
        }

        mProgressDialog.setCancelable(true);
        mProgressDialog.setCanceledOnTouchOutside(cancelable);
        if (!cancelable) {
            mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    onBackPressed();
                }
            });
        }
        mProgressDialog.setMessage(message);
        try {
            if (!mProgressDialog.isShowing()) {
                progressDialogShow = true;
                Log.i(TAG, message);
                mProgressDialog.show();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

    }

    public void dismissProgressDialog(String msg) {
        timeoutMsg = msg;
        if (mProgressDialog != null) {
            Log.i(TAG, "is show?" + mProgressDialog.isShowing());
        } else {
            Log.i(TAG, "progress is null");
        }

        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            try {
                mProgressDialog.dismiss();
                progressDialogShow = false;
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }

        }
        if (!TextUtils.isEmpty(msg)) {
            showToast(msg);
        }
    }


    public void timeOut(int delay, final String msg) {
        timeoutMsg = msg;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (progressDialogShow && timeoutMsg.equals(msg))
                    dismissProgressDialog(msg);
            }
        }, delay);
    }

    protected void menuHomeClick() {
        //默认返回上一层
        finish();
        overridePendingTransition(0, R.anim.slide_left_to_right);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.slide_left_to_right);
    }

    public void showToast(String msg) {
        System.out.println(msg);
        try {
            tv_toast.setText(msg);
            toast.setView(toastView);
            toast.show();
        } catch (Exception e) {

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (msgReceiver != null) {
            Log.i(TAG,"UNREGISTER RECEIVER");
            unregisterReceiver(msgReceiver);
            msgReceiver = null;
        }

    }

//    public void showTestToast(String msg) {
//
//        try {
//            if (!application.settingEntity.isTest()) return;
//            tv_toast.setText(msg);
//            toast.setView(toastView);
//            toast.show();
//        } catch (Exception e) {
//
//        }
//
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            exit();
            //  showToast("请勿退出!");
            //   return false;
        }

        return false;
    }

    public void exit() {
        if (!isExit) {
            isExit = true;
            showToast(getResources().getString(R.string.press_again_exit));
            EventMessage msg = new EventMessage();
            msg.setAction("setting");

            EventBus.getDefault().postSticky(msg);
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            //  ActivityMgr.getInstance().exitApp();
            System.exit(0);
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    public String getCurrentDateStr() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public String getDateStryyMMdd(int year, int monthOfYear, int dayOfMonth) {
        int unitPlace = year / 1 % 10;
        int tenPlace = year / 10 % 10;
        StringBuilder sb = new StringBuilder();
        sb.append(tenPlace);
        sb.append(unitPlace);
        monthOfYear = monthOfYear + 1;
        if (monthOfYear < 10) {
            sb.append("0" + monthOfYear);
        } else {
            sb.append(monthOfYear);
        }
        if (dayOfMonth < 10) {
            sb.append("0" + dayOfMonth);
        } else {
            sb.append(dayOfMonth);
        }
        return sb.toString();
    }

    public String getDateStryyyyMMdd(int year, int monthOfYear, int dayOfMonth) {

        StringBuilder sb = new StringBuilder();
        sb.append(year);

        monthOfYear = monthOfYear + 1;
        if (monthOfYear < 10) {
            sb.append("0" + monthOfYear);
        } else {
            sb.append(monthOfYear);
        }
        if (dayOfMonth < 10) {
            sb.append("0" + dayOfMonth);
        } else {
            sb.append(dayOfMonth);
        }
        return sb.toString();
    }

    public String getDateStr(int year, int monthOfYear, int dayOfMonth, String format) {
        Date date = new Date();
        date.setYear(year);
        date.setMonth(monthOfYear);
        date.setDate(dayOfMonth);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public void showDatePicker(MyDatePickerDialog.OnDateSetListener listener) {
        MyDatePickerDialog myDatePickerDialog = new MyDatePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
                listener, year, monthOfYear, dayOfMonth);

        myDatePickerDialog.myShow();
    }

    public void hideInputKeyboard() {
//        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        if (getCurrentFocus() != null) {
            Log.i(TAG, "hideInputKeyboard");
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } else {
            Log.i(TAG, "getCurrentFocus() is null");
        }
    }

    public void hideInputKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);

            Log.i(TAG, "InputMethodManager ok");
        } else {
            Log.i(TAG, "InputMethodManager is null");
        }
    }

    protected boolean isServiceWork(Context mContext, String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM
                .getRunningServices(40);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();

            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        Log.e(TAG, "Service is work: " + isWork);
        return isWork;
    }

    public void restartMqttService() {

        if (Utils.isServiceWork(BaseActivity.this, "tech.treec.smartlight.service.MQTTService")) {
//            Intent serviceIntent = new Intent(BaseActivity.this, MQTTService.class);
//            stopService(serviceIntent);
            Log.i(TAG, "MQTT服务已启动");
            return;
        }

        Intent svc = new Intent(BaseActivity.this, MQTTService.class);
        startService(svc);
//        Log.i(TAG, "Service is run!");
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                try {
//
////                    Intent intent = new Intent();
////                    intent.setAction(MQTT2Service.ACTION_MQTTSERVICE_RECEIVE);
////                    intent.putExtra("MessageType", 0);
////                    sendBroadcast(intent);
////                    Log.i(TAG, "发送消息");
//
//
////                    MqttManager.setUrl("tcp://192.168.119.61:1883");
////                    Log.i(TAG, "SET");
////                    MqttManager.disconnect();
////                    mqtt.connect("xxx", "xx");
////                    String subTopic = "/light/DARMAO0005880000/data/report";
////                    MqttManager.subscribe(subTopic);
////
////                    String cmdTopic = "/light/DARMAO0005880000/cmd/report";
////                    JSONObject json = new JSONObject();
////                    json.put("cmd", "reportAll");
////                    Log.i(TAG, "SET： " + json.toString());
////                    MqttManager.publish(cmdTopic, json.toString());
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        }, 5000);
//        Handler handler1 = new Handler();
//        handler1.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Log.i(TAG, "RUNXXX");
//                String MqttMessage = "{\"ctrlPanelId\":\"DARMAO0005880000\",\"light\":[{\"lightStatus\":0,\"deviceId\":\"588_1f793031_2\",\"deviceName\":\"一位面板201@\"},{\"lightStatus\":0,\"deviceId\":\"588_7d163031_2\",\"deviceName\":\"一位面板202@\"},{\"lightStatus\":0,\"deviceId\":\"588_77623032_1\",\"deviceName\":\"230前\"},{\"lightStatus\":0,\"deviceId\":\"588_77623032_3\",\"deviceName\":\"230后\"},{\"lightStatus\":0,\"deviceId\":\"588_10333032_1\",\"deviceName\":\"228前\"},{\"lightStatus\":0,\"deviceId\":\"588_10333032_3\",\"deviceName\":\"228后\"},{\"lightStatus\":0,\"deviceId\":\"588_055d3031_2\",\"deviceName\":\"一位面板221@\"},{\"lightStatus\":0,\"deviceId\":\"588_617e3031_2\",\"deviceName\":\"一位面板222@\"},{\"lightStatus\":0,\"deviceId\":\"588_1b1c3063_1\",\"deviceName\":\"1路233\"},{\"lightStatus\":0,\"deviceId\":\"588_1b1c3063_2\",\"deviceName\":\"2路233\"},{\"lightStatus\":0,\"deviceId\":\"588_1b1c3063_3\",\"deviceName\":\"3路233\"},{\"lightStatus\":0,\"deviceId\":\"588_1b1c3063_4\",\"deviceName\":\"4路233\"},{\"lightStatus\":0,\"deviceId\":\"588_22783031_2\",\"deviceName\":\"一位面板224@\"},{\"lightStatus\":0,\"deviceId\":\"588_73663031_2\",\"deviceName\":\"一位面板225@\"},{\"lightStatus\":0,\"deviceId\":\"588_5a1b3031_2\",\"deviceName\":\"一位面板226@\"},{\"lightStatus\":0,\"deviceId\":\"588_082a3031_2\",\"deviceName\":\"一位面板227@\"},{\"lightStatus\":1,\"deviceId\":\"588_57383063_1\",\"deviceName\":\"1路茶水间\"},{\"lightStatus\":1,\"deviceId\":\"588_57383063_2\",\"deviceName\":\"2路茶水间\"},{\"lightStatus\":0,\"deviceId\":\"588_57383063_3\",\"deviceName\":\"3路茶水间\"},{\"lightStatus\":1,\"deviceId\":\"588_57383063_4\",\"deviceName\":\"4路茶水间\"},{\"lightStatus\":0,\"deviceId\":\"588_640f3032_1\",\"deviceName\":\"232前\"},{\"lightStatus\":1,\"deviceId\":\"588_640f3032_3\",\"deviceName\":\"232后\"},{\"lightStatus\":1,\"deviceId\":\"588_34643063_1\",\"deviceName\":\"B1B2B3(2-15)\"},{\"lightStatus\":1,\"deviceId\":\"588_34643063_2\",\"deviceName\":\"B4B5(2-15)\"},{\"lightStatus\":1,\"deviceId\":\"588_34643063_3\",\"deviceName\":\"B6B7(2-15)\"},{\"lightStatus\":1,\"deviceId\":\"588_34643063_4\",\"deviceName\":\"B区(筒灯)\"},{\"lightStatus\":1,\"deviceId\":\"588_77733063_1\",\"deviceName\":\"B10B11(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_77733063_2\",\"deviceName\":\"B8B9(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_77733063_3\",\"deviceName\":\"B14B15(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_77733063_4\",\"deviceName\":\"B12B13(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_440f3063_1\",\"deviceName\":\"B14B15(1-11)\"},{\"lightStatus\":1,\"deviceId\":\"588_440f3063_2\",\"deviceName\":\"B12B13(1-11)\"},{\"lightStatus\":1,\"deviceId\":\"588_440f3063_3\",\"deviceName\":\"B10B11(1-11)\"},{\"lightStatus\":1,\"deviceId\":\"588_440f3063_4\",\"deviceName\":\"B8B9(1-6)\"},{\"lightStatus\":1,\"deviceId\":\"588_2f6d3032_1\",\"deviceName\":\"B区筒灯5\"},{\"lightStatus\":0,\"deviceId\":\"588_2f6d3032_3\",\"deviceName\":\"B区筒灯6\"},{\"lightStatus\":1,\"deviceId\":\"588_44523063_1\",\"deviceName\":\"北侧条灯\"},{\"lightStatus\":1,\"deviceId\":\"588_44523063_2\",\"deviceName\":\"北侧筒灯\"},{\"lightStatus\":1,\"deviceId\":\"588_44523063_3\",\"deviceName\":\"南侧条灯\"},{\"lightStatus\":1,\"deviceId\":\"588_44523063_4\",\"deviceName\":\"南侧筒灯\"},{\"lightStatus\":1,\"deviceId\":\"588_5b3f3063_1\",\"deviceName\":\"B13B14(24-32)\"},{\"lightStatus\":1,\"deviceId\":\"588_5b3f3063_2\",\"deviceName\":\"B11B12(24-32)\"},{\"lightStatus\":0,\"deviceId\":\"588_5b3f3063_3\",\"deviceName\":\"B区筒灯1\"},{\"lightStatus\":1,\"deviceId\":\"588_5b3f3063_4\",\"deviceName\":\"B区筒灯2\"},{\"lightStatus\":1,\"deviceId\":\"588_61263063_1\",\"deviceName\":\"空\"},{\"lightStatus\":1,\"deviceId\":\"588_61263063_2\",\"deviceName\":\"B10B1(24-32)\"},{\"lightStatus\":1,\"deviceId\":\"588_61263063_3\",\"deviceName\":\"B9(24-32)\"},{\"lightStatus\":1,\"deviceId\":\"588_61263063_4\",\"deviceName\":\"B8(19-27)\"},{\"lightStatus\":1,\"deviceId\":\"588_17573063_1\",\"deviceName\":\"B6B7(16-25)\"},{\"lightStatus\":1,\"deviceId\":\"588_17573063_2\",\"deviceName\":\"B4B5(16-25)\"},{\"lightStatus\":1,\"deviceId\":\"588_17573063_3\",\"deviceName\":\"B2B3(16--25)\"},{\"lightStatus\":1,\"deviceId\":\"588_17573063_4\",\"deviceName\":\"B1(16-厂办)\"},{\"lightStatus\":1,\"deviceId\":\"588_61013031_2\",\"deviceName\":\"一位面板卫门灯\"},{\"lightStatus\":1,\"deviceId\":\"588_62633032_1\",\"deviceName\":\"女卫前\"},{\"lightStatus\":0,\"deviceId\":\"588_62633032_3\",\"deviceName\":\"女卫后\"},{\"lightStatus\":1,\"deviceId\":\"588_03533032_1\",\"deviceName\":\"1路男卫\"},{\"lightStatus\":0,\"deviceId\":\"588_03533032_3\",\"deviceName\":\"2路男卫\"},{\"lightStatus\":1,\"deviceId\":\"588_4b243033_1\",\"deviceName\":\"1路男卫\"},{\"lightStatus\":1,\"deviceId\":\"588_4b243033_2\",\"deviceName\":\"2路男卫\"},{\"lightStatus\":1,\"deviceId\":\"588_4b243033_3\",\"deviceName\":\"3路男卫\"},{\"lightStatus\":1,\"deviceId\":\"588_48623063_1\",\"deviceName\":\"前排照明灯\"},{\"lightStatus\":1,\"deviceId\":\"588_48623063_2\",\"deviceName\":\"后排照明灯\"},{\"lightStatus\":1,\"deviceId\":\"588_48623063_3\",\"deviceName\":\"电梯厅照明灯\"},{\"lightStatus\":1,\"deviceId\":\"588_48623063_4\",\"deviceName\":\"楼梯照明灯\"},{\"lightStatus\":0,\"deviceId\":\"588_6c583063_1\",\"deviceName\":\"B20(01-11)\"},{\"lightStatus\":0,\"deviceId\":\"588_6c583063_2\",\"deviceName\":\"B18B19(01-11)\"},{\"lightStatus\":0,\"deviceId\":\"588_6c583063_3\",\"deviceName\":\"B16B17(1-11)\"},{\"lightStatus\":1,\"deviceId\":\"588_6c583063_4\",\"deviceName\":\"空\"},{\"lightStatus\":0,\"deviceId\":\"588_133a3031_2\",\"deviceName\":\"一位面板219@\"},{\"lightStatus\":1,\"deviceId\":\"588_4b173063_1\",\"deviceName\":\"B16B17(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_4b173063_2\",\"deviceName\":\"B18B19(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_4b173063_3\",\"deviceName\":\"B20(13-22)\"},{\"lightStatus\":1,\"deviceId\":\"588_4b173063_4\",\"deviceName\":\"B区筒灯\"}],\"body\":[{\"lightStatus\":1,\"deviceId\":\"588_2f030401\",\"deviceName\":\"微波传感230@\"},{\"lightStatus\":1,\"deviceId\":\"588_36750401\",\"deviceName\":\"微波传感231@\"},{\"lightStatus\":1,\"deviceId\":\"588_6b320401\",\"deviceName\":\"微波传感232—1@\"},{\"lightStatus\":0,\"deviceId\":\"588_00550401\",\"deviceName\":\"微波传感233—2@\"},{\"lightStatus\":0,\"deviceId\":\"588_3d4a0401\",\"deviceName\":\"微波传感225@\"},{\"lightStatus\":1,\"deviceId\":\"588_285f0401\",\"deviceName\":\"微波传感226@\"},{\"lightStatus\":1,\"deviceId\":\"588_48690401\",\"deviceName\":\"微波传感227@\"},{\"lightStatus\":1,\"deviceId\":\"588_435a0401\",\"deviceName\":\"微波传感228@\"},{\"lightStatus\":1,\"deviceId\":\"588_150b0401\",\"deviceName\":\"微波传感222@\"},{\"lightStatus\":1,\"deviceId\":\"588_35020401\",\"deviceName\":\"微波传感221@\"},{\"lightStatus\":1,\"deviceId\":\"588_6d2c0401\",\"deviceName\":\"微波传感202@\"},{\"lightStatus\":1,\"deviceId\":\"588_153c0401\",\"deviceName\":\"微波传感201@\"},{\"lightStatus\":0,\"deviceId\":\"588_170d0401\",\"deviceName\":\"微波传感233—1@\"},{\"lightStatus\":1,\"deviceId\":\"588_181c0401\",\"deviceName\":\"微波传感232—2@\"},{\"lightStatus\":1,\"deviceId\":\"588_651b0401\",\"deviceName\":\"微波传感224@\"},{\"lightStatus\":1,\"deviceId\":\"588_0f4a0401\",\"deviceName\":\"微波传感219@\"},{\"lightStatus\":0,\"deviceId\":\"588_57230401\",\"deviceName\":\"微波传感228-2\"}]}";
//                String topic = "/light/DARMAO0005880000/data/report";
//                Intent intent = new Intent();
//                intent.setAction(MQTT1Service.ACTION_MQTTSERVICE_RECEIVE);
//                intent.putExtra("MessageType", 1);
//                intent.putExtra("Topic", topic);
//                intent.putExtra("MqttMessage", MqttMessage);
//
//                sendBroadcast(intent);
//                Log.i(TAG, "发送消息");
//            }
//        }, 15000);
    }

    private class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "消息到达");
            try {
                int flag = intent.getIntExtra("ACTION_TYPE", -1);

                switch (flag) {
                    case ACTION_MQTT_RESTART_SERVICE:
                        restartMqttService();
                        break;
                    default:
                        break;
                }

            } catch (Exception e) {
                Log.e(TAG, "MsgReceiver error: " + e.getMessage());
            }
        }
    }


}
